package com.Crawlzongheng;

import java.util.HashMap;

import com.CrawlData.CrawlBase;
import com.Model.NovelReadMode;
import com.Util.ParseMD5;
import com.Util.RegexUtil;
import com.Util.jsonUtil;
import com.zonghengdb.ZongHengDB;
/**
 * 阅读页信息采集
 * @author john
 *
 */
public class ReadPage extends CrawlBase{
	private String url;
	private static HashMap<String, String> params;
									//<em itemprop="headline">084.胜负已定</em>
	private static final String TITLE = "<em itemprop=\"headline\">(.*?)</em>";
	private static final String WORDCOUNT = "<span itemprop=\"wordCount\">(.*?)</span>";
	private static final String CONTENT = "<div id=\"chapterContent\" class=\"content\" itemprop=\"acticleBody\">(.*?)</div>";
	static {
		params = new HashMap<String, String>();
		params.put("Referer", "http://book.zongheng.com");
		params.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36");
		params.put("Host", "book.zongheng.com");
	}
	/**
	 * 获取小说阅读页的信息
	 * @return
	 */
	public NovelReadMode analyzer(){
		NovelReadMode bean = new NovelReadMode();
		bean.setUrl(this.url);
		bean.setId(ParseMD5.parseStrToMD5(bean.getUrl()));
		bean.setTitle(getTitle());
		bean.setWordCount(getWordCount());
		bean.setContent(getContent());
		return bean;
	}
	
	public ReadPage(String url) {
		readPageByGet(url, params, "utf-8");
		this.url=url;
	}
	//获取小说的标题
	private String getTitle(){
		return RegexUtil.getFirstString(getPageSourceCode(), TITLE, 1);
	}
	//获取小说的字数
	private int getWordCount(){
		String wordCountStr = RegexUtil.getFirstString(getPageSourceCode(), WORDCOUNT, 1);
		return Integer.parseInt(wordCountStr);
	}
	//获取小说的内容
	private String getContent(){
		return RegexUtil.getFirstString(getPageSourceCode(), CONTENT, 1);
	}
	public static void main(String[] args) {

		ReadPage readPage = new ReadPage("http://book.zongheng.com/chapter/359388/6095894.html");
		
		
		NovelReadMode bean = readPage.analyzer();
		bean.setChapterId(84);
		bean.setChapterTime(1402382221000l);
		ZongHengDB db = new ZongHengDB();
		db.saveNovelRead(bean);
	}

}
